<template>
  <div class="register-page">
    <!-- 注册页 -->
    <!-- 1.顶部标题 -->
    <van-nav-bar title="面经注册" />
    <!-- 2.注册表单内容 -->
    <van-form @submit="onSubmit">
    <!-- \w 表示匹配任何字母、数字或下划线字符（即 A-Z、a-z、0-9 和下划线_） -->
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="请输入用户名"
        :rules="[
          { required: true, message: '请输入用户名' },
          { pattern: /^\w{6,15}$/, message: '用户名必须在6-15位' },
        ]"
      ></van-field>
      <van-field
        v-model="password"
        name="password"
        label="密码"
        placeholder="请输入密码"
        type="password"
        :rules="[
          { required: true, message: '请输入密码' },
          { pattern: /^\w{6,15}$/, message: '密码必须在6-15位' },
        ]"
      ></van-field>
      <div style="margin: 16px">
        <van-button type="primary" round block native-type="submit" loading-text="注册中..." :loading="isRegister" :disabled="isRegister"
          >注册</van-button
        >
      </div>
    </van-form>
    <!-- 3.底部 -->
    <a class="link" href="#/login">已有账号，去登录</a>
  </div>
</template>

<script>
import { UserRegister } from '@/api/user'
export default {
  name: 'RegisterPage',
  data () {
    return {
      username: '',
      password: '',
      isRegister: false
    }
  },
  methods: {
    async onSubmit (value) {
      this.isRegister = true
      // 发送注册请求
      const res = await UserRegister(value)
      console.log(res)
      // 提示+跳转登录页
      this.$toast.success('注册成功')
      this.$router.push('/login')
      this.isRegister = false
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
